package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Hashrate;
import com.ipfsemc.ipfsemc.service.IHashrateService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "HashrateController", tags = "算力")

@RequestMapping("hashrate")
public class HashrateController {
    private static final Logger logger = LoggerFactory.getLogger(HashrateController.class);

    @Autowired
    private IHashrateService hashrateService;

    @ApiOperation(value = "算力添加",httpMethod = "POST")
    @RequestMapping(value = "addHashrate",method = RequestMethod.POST)
    public ResponseBase addHashrate(@RequestBody  Hashrate hashrate) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == hashrate.getHashrateAdd() || null == hashrate.getMillTotal()) {
                jsonData.setMessage("参数异常！");
            } else {

                int add = hashrateService.add(hashrate);
                if (add < 0) {
                    jsonData.setMessage("操作失败！请稍后重试！");
                } else {
                    jsonData.setMessage("已提交！");
                    jsonData.setCode(0);
                }
            }

        } catch (Exception ex) {
            logger.error("[HashrateController][applyAdd]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "算力详情",httpMethod = "GET",notes = "只传有说明的参数")

    @RequestMapping(value = "listHashrate",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="hashrateTime",value="开始时间",required=false,dataType="Date"),
            @ApiImplicitParam(name="endTime",value="结束时间",required=false,dataType="Date"),
    })
    public RespList<Hashrate> listHashrate(@ApiModelPropertyIgnore(
            {"average","hashrateAdd","id","millTotal"}) Hashrate hashrate) {
        RespList jsonData = new RespList();
        try {

            List<Hashrate> hashrates = hashrateService.listHashrate(hashrate);
            jsonData.setResult(hashrates);
            jsonData.setCode(0);
        } catch (Exception ex) {
            logger.error("[HashrateController][listHashrate]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }
}
